package com.divemotor.ventarepuestosinternet.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "GEN_VW_MODELO_VEH")
@NamedQueries({
		@NamedQuery(name="Modelo.comboModelos", 
					query="SELECT new com.divemotor.ventarepuestosinternet.beans.adapter.ModeloAdapter(p.modeloPK.idModelo,p.modeloPK.idMarca,p.modeloPK.idTipoVehiculo,p.nombre) " +
						  "FROM Modelo p order by p.nombre asc"),
		@NamedQuery(name="Modelo.comboModelosPorMarca", 
					query="SELECT new com.divemotor.ventarepuestosinternet.beans.adapter.ModeloAdapter(p.modeloPK.idModelo,p.modeloPK.idMarca,p.modeloPK.idTipoVehiculo,p.nombre) " +
						  "FROM Modelo p Where p.marca.codigoMarca = :marca  order by p.nombre asc")
})
public class Modelo implements Serializable {

	private static final long serialVersionUID = 1L;

	@EmbeddedId
	private ModeloPK modeloPK;
	@Column(name = "des_modelo_veh", length = 100)
	private String nombre;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "COD_MARCA", referencedColumnName="COD_MARCA", insertable = false, updatable = false)
	private Marca marca;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "COD_TIPO_VEHICULO", referencedColumnName="COD_TIPO_VEHICULO", insertable = false, updatable = false)
	private TipoVehiculo tipoVehiculo;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumns({@JoinColumn(name = "COD_MODELO_FABRICA", referencedColumnName = "COD_MODELO_FABRICA", insertable = false, updatable = false),
				@JoinColumn(name = "COD_MARCA", referencedColumnName = "COD_MARCA", insertable = false, updatable = false),
				@JoinColumn(name = "COD_TIPO_COMPONENTE", referencedColumnName = "COD_TIPO_COMPONENTE", insertable = false, updatable = false)})
	private ModeloFabrica modeloFabrica;
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "modelo", fetch=FetchType.LAZY)
	private List<VehiculoExtra> listaVehiculoExtra;
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "modelo", fetch=FetchType.LAZY)
	private List<Vehiculo> listaVehiculo;
	
	public Modelo() {
		this.modeloPK = new ModeloPK();
		this.marca = new Marca();
		this.tipoVehiculo = new TipoVehiculo();
	}
	
	public Modelo(String idModelo, String idMarca, String idTipoVehiculo, String nombre) {
		this.modeloPK = new ModeloPK(idModelo, idMarca, idTipoVehiculo);
		this.nombre = nombre;
	}
	
	public Modelo(String idModelo, String idMarca, String idTipoVehiculo) {
		this.modeloPK = new ModeloPK(idModelo, idMarca, idTipoVehiculo);
	}

	public ModeloPK getModeloPK() {
		return modeloPK;
	}

	public void setModeloPK(ModeloPK modeloPK) {
		this.modeloPK = modeloPK;
	}

	public Marca getMarca() {
		return marca;
	}

	public void setMarca(Marca marca) {
		this.marca = marca;
	}

	public TipoVehiculo getTipoVehiculo() {
		return tipoVehiculo;
	}

	public void setTipoVehiculo(TipoVehiculo tipoVehiculo) {
		this.tipoVehiculo = tipoVehiculo;
	}

	public ModeloFabrica getModeloFabrica() {
		return modeloFabrica;
	}

	public void setModeloFabrica(ModeloFabrica modeloFabrica) {
		this.modeloFabrica = modeloFabrica;
	}

	public List<VehiculoExtra> getListaVehiculoExtra() {
		return listaVehiculoExtra;
	}

	public void setListaVehiculoExtra(List<VehiculoExtra> listaVehiculoExtra) {
		this.listaVehiculoExtra = listaVehiculoExtra;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public List<Vehiculo> getListaVehiculo() {
		return listaVehiculo;
	}

	public void setListaVehiculo(List<Vehiculo> listaVehiculo) {
		this.listaVehiculo = listaVehiculo;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((modeloPK == null) ? 0 : modeloPK.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		Modelo other = (Modelo) obj;
		if (modeloPK == null) {
			if (other.modeloPK != null){
				return false;
			}
		} else if (!modeloPK.equals(other.modeloPK)){
			return false;
		}
		return true;
	}
	
	
}
